#!/bin/sh
set +x
mntpt=$1
orasid=$2
archmnt=$3
tstamp=$4
dbfsPath=$5
flag=$6

if [ "$#" -lt 4 ]; then
 echo "This script needs 4 input parameter"
 echo "usage: recoverRman.sh <Backup location mount path> <New database sid name> <Log Backup location mount path> <timestamp yyyy-mm-dd..hh24:mi:ss>"
exit 1
fi

if [ ! -z "$DB_TZ" ]; then
echo " ********** setting TZ:$DB_TZ  ***************"
TZ=$DB_TZ
export TZ
fi

if [ -z "$rmanchannels" ]; then
rmanchannels=4;
fi

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

allocatech=
releasech=
i=0
while [ $i -lt "$rmanchannels" ]; do
if [ -z "$allocatech" ]; then
allocatech="allocate channel d$i device type disk;"
releasech="release channel d$i;"
else
allocatech=$allocatech"allocate channel d$i device type disk;"
releasech=$releasech"release channel d$i;"
fi
i=`expr $i + 1`
done

if [ ! -f "$dbfsPath" ]; then
  roTBS=''
else
roTBS=`cat $dbfsPath`
fi

if [ ! -z "$roTBS" ]; then
recoverCMD="skip tablespace $roTBS"
else
recoverCMD=""
fi

. ${scriptsRoot}/oracle/oracleclone/OracleAppMountComFunc.sh

export ORACLE_SID=$orasid
dbrecover="dbrecover_"$orasid".txt"
cstrfile="catalogArchivelog_"$orasid".txt"
cstrfiletmp="catalogArchivelogTmp_"$orasid".txt"
echo "******* recovering database *********** "

if [ -f $cstrfile ]; then
 rm -f $cstrfile
fi

if [ ! -z "$archmnt" ]; then
     dirs=`ls -d $archmnt*`
     catalogstr=""
     for d in ${dirs}; do
         archmntpt=$d"/archivelog/"
         # find $archmntpt -mtime -"$imageLogOffset" -type f >> $cstrfile
         find $archmntpt -mtime -type f >> $cstrfile
     done

     sed -e "s/^/catalog BACKUPPIECE '/" $cstrfile > $cstrfiletmp
     mv $cstrfiletmp $cstrfile
     sed  "s/$/' ;/" $cstrfile > $cstrfiletmp
     mv $cstrfiletmp $cstrfile
     if [ "$flag" != "no" ]; then
        catalogstr=`cat $cstrfile`
     else
        catalogstr=''
     fi

   if [ ! -z "$tstamp" ]; then
    if [ "$pdbcnt" -gt "0" ] && [ "$ischild" = "true" ]; then
        rmancmd="run
        {
	    $allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $catalogstr
        recover database until time \"to_date('$tstamp','yyyy-mm-dd..hh24:mi:ss')\" skip tablespace \"pdb\$seed\":SYSTEM, \"pdb\$seed\":SYSAUX, \"pdb\$seed\":ADMIN, \"pdb\$seed\":USERS;
	    $releasech
        }"
   else
        rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $catalogstr
        recover database until time \"to_date('$tstamp','yyyy-mm-dd..hh24:mi:ss')\" $recoverCMD;
	$releasech
        }"
    fi
    else
         rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $catalogstr
        recover database $recoverCMD;
	$releasech
        }"
   fi
else
        rmancmd="run
        {
  	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        recover database $recoverCMD;
	$releasech
        }"
fi

echo $rmancmd
export NLS_DATE_FORMAT="yyyy-mm-dd..hh24:mi:ss"
export NLS_DATE_LANGUAGE=ENGLISH
export NLS_LANG=''
$ORACLE_HOME/bin/rman target / log=$dbrecover << EOF
sql "alter session set NLS_DATE_FORMAT=''yyyy-mm-dd..hh24:mi:ss''";
sql "alter session set NLS_DATE_LANGUAGE=''ENGLISH''";
$rmancmd
exit
EOF

if [ -f $cstrfile ]; then
 rm -f $cstrfile
fi
